﻿using System;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;

namespace XeoEngine.Screens
{
    class HelpBox
    {
        int lifetime, maxlifetime;
        string message;
        class bt
        {
            public Point pos;
            public Texture2D tex;
        }
        public bool visible;
        List<bt> buttons = new List<bt>();
        SpriteFont font;
        ContentManager content;
        Point pos;
        Point drawpos;
        Texture2D bg, br;
        Vector2 size;
        ScreenManager scrm;
        bool soundplayed = false;

        public HelpBox(ContentManager cnt)
        {
            content = cnt;
            font = content.Load<SpriteFont>("Menu/helpfont");
            bg = content.Load<Texture2D>("Menu/gradient");
            br = content.Load<Texture2D>("Menu/border");
        }

        public void Show(ScreenManager sm, string text)
        {
            Show(sm, text, 300, new Point(10, 20));
        }
        public void Show(ScreenManager sm, string text, Point p)
        {
            Show(sm, text, 300, p);
        }
        public void Show(ScreenManager sm, string text, int mlt)
        {
            Show(sm, text, mlt, new Point(10, 20));
        }
        public void Show(ScreenManager sm, string text, int mlt, Point p)
        {
            soundplayed = false;
            scrm = sm;
            int VH = sm.GraphicsDevice.Viewport.Bounds.Height;
            lifetime = 0;
            maxlifetime = mlt;
            visible = true;
            message = null;
            buttons.Clear();
            pos = p;
            text = text.Replace("#n", "\n");
            string[] stra = text.Split(' ');
            for (int i = 0; i < stra.Length; i++)
            {
                string str = stra[i];
                if (str.StartsWith("#k"))
                {
                    bt b = new bt();
                    b.tex = content.Load<Texture2D>("Menu/Buttons/" + str.Remove(0, 2));
                    string[] mln = message.Split('\n');
                    b.pos.Y = mln.Length * 40 - 40;
                    b.pos.X = (int)font.MeasureString(mln[mln.Length - 1]).X + 10;
                    for (int j = 0; j <= b.tex.Width / 15; j++)
                        message += " ";
                    buttons.Add(b);
                }
                else if (str.StartsWith("#t"))
                {
                    bt b = new bt();
                    b.tex = content.Load<Texture2D>(str.Remove(0, 2));
                    string[] mln = message.Split('\n');
                    b.pos.Y = mln.Length * 40 - 40;
                    b.pos.X = (int)font.MeasureString(mln[mln.Length - 1]).X + 10;
                    for (int j = 0; j <= b.tex.Width / 15; j++)
                        message += " ";
                    buttons.Add(b);
                }
                else
                {
                    message += " " + str;
                }
            }
            message = message.Remove(0, 1);
            size = font.MeasureString(message);
            drawpos.X = -(int)size.X;
            drawpos.Y = VH - (int)size.Y - 20;
        }

        public void Update(GameTime gameTime)
        {
            if (lifetime >= maxlifetime)
            {
                visible = false;
                soundplayed = false;
            }
            else
            {
                lifetime++;
                if (maxlifetime - lifetime <= (int)size.X / 15)
                {
                    drawpos.X -= 15;
                }
                else
                {
                    if (drawpos.X < pos.X)
                        drawpos.X += 15;
                    if (drawpos.X > pos.X)
                    {
                        if (!soundplayed)
                        {
                            scrm.soundBank.PlayCue("helptip");
                            soundplayed = true;
                        }
                        drawpos.X--;
                    }
                }
            }
        }

        public void Hide()
        {
            lifetime = maxlifetime - pos.X / 15;
        }

        public void Draw(SpriteBatch sb)
        {
            if (visible)
            {
                sb.Begin();
                int alpha = Math.Min(255, 255 - pos.X + drawpos.X);
                sb.Draw(bg, new Rectangle(drawpos.X - 10, drawpos.Y - 10, (int)size.X + 20, (int)size.Y + 20), new Color(255, 255, 255, alpha - 55));
                sb.DrawString(font, message, new Vector2(drawpos.X, drawpos.Y), new Color(0, 170, 255, alpha));
                foreach (bt b in buttons)               
                    sb.Draw(b.tex, new Vector2(drawpos.X + b.pos.X, drawpos.Y + b.pos.Y), null, new Color(255, 255, 255, alpha), 0.0f, Vector2.Zero, 40.0f / b.tex.Height, SpriteEffects.None, 0.0f);
                sb.Draw(br, new Rectangle(drawpos.X - 14, drawpos.Y - 10, (int)size.X + 28, 5), Color.White);
                sb.Draw(br, new Rectangle(drawpos.X - 14, drawpos.Y + (int)size.Y + 10, (int)size.X + 28, 5), Color.White);
                sb.End();
            }
        }
    }
}